package com.lifesting.workflow.server;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import com.lifesting.workflow.runtime.service.WfsService;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {

	private ServiceTracker wfsServiceTracker;
	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
	 */
	public Object start(IApplicationContext context) throws Exception {
		testSample();
		return IApplication.EXIT_OK;
	}

	private void testSample() {
		String sample_default = "/com.lifesting.workflow.server/sample/default.gd";
		BundleContext bundleContext = Activator.getDefault().getBundle().getBundleContext();
		wfsServiceTracker = new ServiceTracker(bundleContext,WfsService.class.getName(),null);
		wfsServiceTracker.open();
		WfsService service = (WfsService) wfsServiceTracker.getService();
		try {
			service.runPluginLoaded(sample_default);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop() {
		// nothing to do
		wfsServiceTracker.close();
		wfsServiceTracker = null;
	}
}
